package com.lj.boss.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lj.boss.commons.base.BaseController;
import com.lj.boss.commons.result.PageInfo;
import com.lj.boss.commons.utils.JsonUtils;
import com.lj.boss.commons.utils.StringUtils;
import com.lj.boss.model.student.ExamRecord;
import com.lj.boss.model.vo.ExamRecordVo;
import com.lj.boss.service.student.IExamRecordService;
@Controller
@RequestMapping("/examRecord")
public class ExamRecordContoller extends BaseController{
	private Logger logger=LoggerFactory.getLogger(ExamRecordContoller.class);
    @Autowired
    private IExamRecordService examRecordService;

    /**
     * 考生管理主页
     *
     * @return
     */
    @GetMapping(value = "/manager")
    public String manager() {
        return "admin/examRecord/examRecord";
    }

    

    /**
     * 考生列表
     *
     * @return
     *//*
    @RequestMapping("/treeGrid")
    @ResponseBody
    public Object treeGrid() {
        return examRecordService.selectTreeGrid();
    }*/

    /**
     * 添加考生页
     *
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage() {
        return "admin/examRecord/examRecordAdd";
    }

    /**
     * 添加考生
     *
     * @param examRecord
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Object add(@Valid ExamRecord examRecord) {
        examRecord.setCreatTime(new Date());
        examRecordService.insert(examRecord);
        return renderSuccess("添加成功！");
    }

    /**
     * 编辑考生页
     *
     * @param request
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        ExamRecord examRecord = examRecordService.selectById(id);
        model.addAttribute("examRecord", examRecord);
        return "admin/examRecord/examRecordEdit";
    }

    /**
     * 编辑考生
     *
     * @param examRecord
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(@Valid ExamRecord examRecord) {
        examRecordService.updateById(examRecord);
        return renderSuccess("编辑成功！");
    }

    /**
     * 删除考生
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        examRecordService.deleteById(id);
        return renderSuccess("删除成功！");
    }
    

    /**
     * 管理列表
     *
     * @param examRecordVo
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @return
     */
    @PostMapping("/dataGrid")
    @ResponseBody
    public Object dataGrid(ExamRecordVo examRecordVo, Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String, Object> condition = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(examRecordVo.getExamineeName())) {
            condition.put("examineeName", examRecordVo.getExamineeName());
        }
        
        if (examRecordVo.getCreatedateStart() != null) {
            condition.put("startTime", examRecordVo.getCreatedateStart());
        }
        if (examRecordVo.getCreatedateEnd() != null) {
            condition.put("endTime", examRecordVo.getCreatedateEnd());
        }
        pageInfo.setCondition(condition);
        examRecordService.selectDataGrid(pageInfo);
        logger.info("controller="+JsonUtils.toJson(pageInfo));
        return pageInfo;
    }


}
